package com.cheyunkeji.er.fragment.fast_evaluate;

import android.content.Intent;
import android.graphics.Color;
import android.os.Build;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.cheyun.netsalev3.MyApplication;
import com.cheyun.netsalev3.R;
import com.cheyunkeji.er.activity.ModuleSelectActivity;
import com.cheyunkeji.er.activity.fast_evaluate.EvaluateRecordActivity;
import com.cheyunkeji.er.activity.fast_evaluate.FastEvaluateActivity;
import com.cheyunkeji.er.activity.fast_evaluate.PrevEvaluateRecordActivity;
import com.cheyunkeji.er.app.PushMsgManager;
import com.cheyunkeji.er.app.UserInfoManager;
import com.cheyunkeji.er.base.BaseFragment;
import com.cheyunkeji.er.bean.PushMsgBean;
import com.cheyunkeji.er.bean.UserInfo;
import com.cheyunkeji.er.utils.ChannelInfoUtil;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by Michael on 2017/4/13 14:54.
 */

public class FastEvaluateMineFragment extends BaseFragment {

    @BindView(R.id.tv_user_name)
    TextView tvUserName;
    @BindView(R.id.tv_tele_number)
    TextView tvTeleNumber;
    @BindView(R.id.civ_photo)
    ImageView civPhoto;
    @BindView(R.id.tv_evaluate_record_count)
    TextView tvEvaluateRecordCount;
    @BindView(R.id.rl_evaluate_record)
    RelativeLayout rlEvaluateRecord;
    @BindView(R.id.rl_perv_evaluate_record)
    RelativeLayout rlPrevEvaluateRecord;
//    @BindView(R.id.rl_switch_channel)
//    RelativeLayout rlSwitchChennel;
//    @BindView(R.id.rl_notices)
//    RelativeLayout rl_notices;
//    @BindView(R.id.rl_call_center)
//    RelativeLayout rl_call_center;
//    @BindView(R.id.btn_logout)
//    Button btnLogout;
//    @BindView(R.id.iv_unread_notify)
//    ImageView ivUnreadNotify;
//    @BindView(R.id.rl_switch_module)
//    RelativeLayout rlSwitchModule;
//    @BindView(R.id.ll_switch_module)
//    LinearLayout llSwitchModule;
//    @BindView(R.id.llys)
//    LinearLayout llys;

    private PushMsgManager pushMsgManager;


    @Override
    protected void doInflaterView(LayoutInflater inflater, ViewGroup container) {

        contentView = inflater.inflate(R.layout.er_frag_fast_evaluate_mine, container, false);
        ButterKnife.bind(this, contentView);
//        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)
//            getActivity().getWindow().setStatusBarColor(Color.TRANSPARENT);
    }

    @Override
    protected void init() {

//        civPhoto.setOnClickListener(this);
        rlEvaluateRecord.setOnClickListener(this);
        rlPrevEvaluateRecord.setOnClickListener(this);
//        rlSwitchChennel.setOnClickListener(this);
//        btnLogout.setOnClickListener(this);
//        rl_notices.setOnClickListener(this);
//        rl_call_center.setOnClickListener(this);
//        llys.setOnClickListener(this);

        EventBus.getDefault().register(this);

    }

    @Override
    protected void initView() {

        UserInfo.ChannelListBean currentChannel = ChannelInfoUtil.getCurrentChannelInfo();
        int moduleCount = currentChannel.getIsarchiver() + currentChannel.getIsauction() + currentChannel.getIsrapid();

//        if (moduleCount != 0 && moduleCount != 1) {
//            llSwitchModule.setVisibility(View.VISIBLE);
//            rlSwitchModule.setOnClickListener(this);
//        }

//        int head = R.drawable.er_evaluate_mine_noman;
//        switch (Integer.parseInt(UserInfoManager.getInstance( MyApplication.getInstance()).getGender())) {
//            case 0:
//                head = R.drawable.er_evaluate_mine_noman;
//                break;
//            case 1:
//                head = R.drawable.er_evaluate_mine_default_photo;
//                break;
//            case 2:
//                head = R.drawable.er_evaluate_mine_woman;
//                break;
//            default:
//                break;
//        }
        civPhoto.setImageResource(R.drawable.d01_wdym_head);

//        String avatar = MySharedPreferences.INSTANCE.getCenterLoginData().getUser().getAvatar();

//        civPhoto.setOnClickListener(this);
        if (UserInfoManager.getInstance(MyApplication.getInstance()).getMobile() != null
                && UserInfoManager.getInstance(MyApplication.getInstance()).getRealName() != null) {
            tvUserName.setText(UserInfoManager.getInstance(MyApplication.getInstance()).getRealName());
            String mobile = UserInfoManager.getInstance(MyApplication.getInstance()).getMobile();
            if(mobile.length()>=11) {
                tvTeleNumber.setText(mobile.replaceFirst(mobile.substring(3, 7), "****"));
            }
        }
    }

    @Override
    public void onResume() {
        super.onResume();
        if (UserInfoManager.getInstance(MyApplication.getInstance()).getMobile() != null
                && UserInfoManager.getInstance(MyApplication.getInstance()).getRealName() != null) {
            tvUserName.setText(UserInfoManager.getInstance(MyApplication.getInstance()).getRealName());
            String mobile = UserInfoManager.getInstance(MyApplication.getInstance()).getMobile();
            if(mobile.length()>=11) {
                tvTeleNumber.setText(mobile.replaceFirst(mobile.substring(3, 7), "****"));
            }
        }
        dealWithPushMsg();
    }

    @Override
    protected void initData() {

    }

    @Override
    public void onClick(View v) {

        Intent intent;
        switch (v.getId()) {
//            case R.id.civ_photo:
//                startActivity(new Intent(getActivity(), EditingInformationActivity.class));
//                break;

            case R.id.rl_evaluate_record:
                startActivity(new Intent(getActivity(), EvaluateRecordActivity.class));
                break;
            case R.id.rl_perv_evaluate_record:
                startActivity(new Intent(getActivity(), PrevEvaluateRecordActivity.class));
                break;
//            case R.id.rl_switch_channel:
//                intent = new Intent(getActivity(), ChannelSelectActivity.class);
//                startActivity(intent);
//                break;

//            case R.id.btn_logout:
//                MyApplication.pwd = UserInfoManager.getInstance(MyApplication.getInstance()).getPwd();
//                MyApplication.phone = UserInfoManager.getInstance(MyApplication.getInstance()).getMobile();
//                UserInfoManager.getInstance(MyApplication.getInstance()).clear();
//                MyApplication.logout(MyApplication.getInstance());
//                break;

//            case R.id.rl_notices:
//
//                intent = new Intent(getActivity(), NoticeActivity.class);
//                startActivity(intent);
//                if (null == pushMsgManager)
//                    pushMsgManager = PushMsgManager.getInstance(MyApplication.getInstance(), ChannelInfoUtil.getCurrentChannelInfo().getChannel() + UserInfoManager.getInstance(this.getActivity()).getAccount());
//                pushMsgManager.clearNotifyMsgRecordCount(PushMsgManager.MSG_TYPE_ZHANNEIXIN);
//
//                break;

//            case R.id.rl_call_center:
//                startActivity(new Intent(getActivity(), CallCenterActivity.class));
//                break;

            case R.id.rl_switch_module:
                startActivity(new Intent(this.getActivity(), ModuleSelectActivity.class));
                break;


            default:
                break;
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onPushMsgReceived(PushMsgBean msgBean) {

        if (msgBean.getNotice_type() == PushMsgManager.MSG_TYPE_ZHANNEIXIN) {

            dealWithPushMsg();
            EventBus.getDefault().removeStickyEvent(msgBean);
        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        EventBus.getDefault().unregister(this);
    }

    private void dealWithPushMsg() {

        if (null == pushMsgManager)
            pushMsgManager = PushMsgManager.getInstance(MyApplication.getInstance(), ChannelInfoUtil.getCurrentChannelInfo().getChannel() + UserInfoManager.getInstance(this.getActivity()).getAccount());

//        int msgCount = pushMsgManager.getMsgCount(PushMsgManager.MSG_TYPE_ZHANNEIXIN);
//        if (msgCount == 0) {
//            ivUnreadNotify.setVisibility(View.GONE);
//        } else {
//            ivUnreadNotify.setVisibility(View.VISIBLE);
//        }
        ((FastEvaluateActivity) getActivity()).refreshBottomMsgCount();
    }

}
